package com.caifu.controller;

import com.caifu.bean.Result;

import com.caifu.pojo.COrgDept;
import com.caifu.pojo.SysUser;
import com.caifu.service.DeptService;
import com.caifu.service.UserDeviceService;
import com.caifu.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 机构部门表 前端控制器 用户端
 * </p>
 *
 * @author tww
 * @since 2020-12-10
 */
@Controller
@RequestMapping("userDept")
public class DeptController extends AbstractController {
    final static Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Resource
    private DeptService deptService;

    @Resource
    private UserDeviceService userDeviceService;

    private String pre = "view/user/";

    /**
     * 部门管理列表页
     */
    @GetMapping("toDeptListPage")
    public String toDeptListPage() {
        return pre + "dept_list";
    }

    /**
     * 部门新增页
     */
    @GetMapping("toDeptAddPage")
    public String toDeptAddPage() {
        return pre + "dept_add";
    }

    /**
     * 获取部门列表 分页
     */
    @GetMapping("getDeptList")
    @ResponseBody
    public Result getDeptList(String searchParam, Integer currentPage, Integer pageSize) {
        SysUser user = getUser();
        try {
            return success(0, "", deptService.getDeptList(searchParam, currentPage, pageSize, user));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 添加机构和机构登录账号
     */
    @PostMapping("addDept")
    @ResponseBody
    public Result addOrUpdate(COrgDept cOrgDept) {
        SysUser user = getUser();
        try {
            return deptService.addOrUpdate(cOrgDept, user);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 跳机构详情页
     */
    @GetMapping("toDeptDetailsPage")
    public String toDeptDetailsPage(String id, Model model) {
        //获取机构详情
        COrgDept cOrgDept = deptService.getDeptById(id);
        //获取管理员账号
        String userId = deptService.getDeptUserId(id);
        model.addAttribute("userId", userId);
        model.addAttribute("dept", cOrgDept);
        model.addAttribute("id", id);
        return pre + "dept_details";
    }


    /**
     * 获取部门所有设备
     */
    @GetMapping("getDevByDeptId")
    @ResponseBody
    public Result getDevByOrgId(Integer id) {
        //获取机构详情
        try {
            return success(200, "", userDeviceService.getDeviceNameByDeptId(id));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据id删除部门
     */
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        //获取机构详情
        try {
            deptService.deleteById(id);
            return success(200, "删除成功");
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 部门重置密码
     */
    @PostMapping("resetPwd")
    @ResponseBody
    public Result resetPwd(Integer id) {
        try {
            return deptService.resetPwd(id);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取机构所有部门
     */
    @GetMapping("getDeptByUser")
    @ResponseBody
    public Result getDeptByUser() {
        SysUser user = getUser();
        //获取机构详情
        try {
            return success(200, "", deptService.getDeptByUser(user));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

}
